package com.example.simplemusicplayer;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private MusicService.MyBinder binder;
    private Mycoon coon;

    private TextView songCurrentTime;
    private TextView songDuration;
    private TextView songName;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        songCurrentTime = findViewById(R.id.song_current_time);
        songDuration = findViewById(R.id.song_duration);
        songName = findViewById(R.id.song_name);
        progressBar = findViewById(R.id.progress_bar);



        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }

        //创建连接对象
        coon = new Mycoon();
        Intent intent = new Intent(this,MusicService.class);
        bindService(intent,coon,BIND_AUTO_CREATE);  //绑定服务
    }

    @Override
    protected void onDestroy() {
        unbindService(coon);
        super.onDestroy();
    }

    /**
     * 相应播放按钮
     * @param view
     */
    public void play(View view) {
        binder.play(this);
        //设置时长
        songDuration.setText(binder.getSongDuration());
        //设置进度条和当前时间
        new PlayMusicTask().execute();
    }

    /**
     * 响应暂停按钮
     * @param view
     */
    public void pause(View view) {
        binder.pause();
        Log.d("MainActivity", "pause: "+binder.getCurrentProgress());
    }

    private class Mycoon implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MusicService.MyBinder)service;  //绑定服务
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    class PlayMusicTask extends AsyncTask<Void,Object,Void>{

        @Override
        protected Void doInBackground(Void... voids) {
            try{
                while (true){
                    int percent = binder.getCurrentProgress();
                    String currentTime = binder.getCurrentTime();
                    publishProgress(percent,currentTime);
                    if (percent>=100){
                        break;
                    }
                }
            } catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Object... values) {
            progressBar.setProgress((Integer) values[0]);
            songCurrentTime.setText((String)values[1]);
            super.onProgressUpdate(values);
        }
    }
}
